package com.gewu.pm.ui.fragment.terminal;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.http.glide.GlideApp;

import java.io.File;

/**
 * 终端详情 - 详情 Fragment
 * 
 * 根据是否有营业执照显示不同内容：
 * - 有营业执照：显示营业执照图、注册名称、营业执照号
 * - 无营业执照：显示上传图片、备注
 */
public final class DetailFragment extends AppFragment<com.gewu.pm.app.AppActivity> {

    private static final String ARG_HAS_LICENSE = "has_license";

    // 基本信息
    private TextView mTerminalNameView;
    private TextView mTerminalCodeView;
    private TextView mCooperationStatusView;
    private TextView mTerminalTypeView;
    private TextView mPaymentTypeView;
    private ImageView mFacadePhotoView;
    private ImageView mStorePhotoView;
    private TextView mFreezerQuantityView;

    // 营业执照
    private TextView mTaxInvoiceView;
    private TextView mHasLicenseView;
    private View mLayoutHasLicenseContent;
    private View mLayoutNoLicenseContent;
    private ImageView mLicensePhotoView;
    private TextView mRegisterNameView;
    private TextView mLicenseNumberView;
    private ImageView mUploadPhotoView;
    private TextView mNoteView;

    // 配送商信息
    private TextView mDeliveryProviderView;
    private TextView mSalespersonView;
    private TextView mSalesAreaView;
    private TextView mDepartmentView;

    // 系统信息
    private TextView mCreateTimeView;
    private TextView mCreatorView;
    private TextView mModifyTimeView;
    private TextView mModifierView;
    private TextView mFirstOrderTimeView;
    private TextView mLastOrderTimeView;

    // 地区信息
    private TextView mLocationAreaView;
    private TextView mDetailedAddressView;
    private TextView mLatLngView;

    private boolean mHasLicense = false;  // 默认无营业执照

    public static DetailFragment newInstance() {
        return new DetailFragment();
    }

    /**
     * 创建实例并传递是否有营业执照
     */
    public static DetailFragment newInstance(boolean hasLicense) {
        DetailFragment fragment = new DetailFragment();
        Bundle args = new Bundle();
        args.putBoolean(ARG_HAS_LICENSE, hasLicense);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 设置是否有营业执照
     */
    public void setHasLicense(boolean hasLicense) {
        mHasLicense = hasLicense;
        if (mLayoutHasLicenseContent != null && mLayoutNoLicenseContent != null) {
            updateLicenseLayout();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.terminal_detail_fragment;
    }

    @Override
    protected void initView() {
        // 基本信息
        mTerminalNameView = findViewById(R.id.tv_terminal_name_info);
        mTerminalCodeView = findViewById(R.id.tv_terminal_code_info);
        mCooperationStatusView = findViewById(R.id.tv_cooperation_status_info);
        mTerminalTypeView = findViewById(R.id.tv_terminal_type_info);
        mPaymentTypeView = findViewById(R.id.tv_payment_type_info);
        mFacadePhotoView = findViewById(R.id.iv_facade_photo);
        mStorePhotoView = findViewById(R.id.iv_store_photo);
        mFreezerQuantityView = findViewById(R.id.tv_freezer_quantity_info);

        // 营业执照
        mTaxInvoiceView = findViewById(R.id.tv_tax_invoice_info);
        mHasLicenseView = findViewById(R.id.tv_has_license_info);
        mLayoutHasLicenseContent = findViewById(R.id.layout_has_license_content);
        mLayoutNoLicenseContent = findViewById(R.id.layout_no_license_content);
        mLicensePhotoView = findViewById(R.id.iv_license_photo);
        mRegisterNameView = findViewById(R.id.tv_register_name_info);
        mLicenseNumberView = findViewById(R.id.tv_license_number_info);
        mUploadPhotoView = findViewById(R.id.iv_upload_photo);
        mNoteView = findViewById(R.id.tv_note_info);

        // 配送商信息
        mDeliveryProviderView = findViewById(R.id.tv_delivery_provider_info);
        mSalespersonView = findViewById(R.id.tv_salesperson_info);
        mSalesAreaView = findViewById(R.id.tv_sales_area_info);
        mDepartmentView = findViewById(R.id.tv_department_info);

        // 系统信息
        mCreateTimeView = findViewById(R.id.tv_create_time);
        mCreatorView = findViewById(R.id.tv_creator);
        mModifyTimeView = findViewById(R.id.tv_modify_time);
        mModifierView = findViewById(R.id.tv_modifier);
        mFirstOrderTimeView = findViewById(R.id.tv_first_order_time);
        mLastOrderTimeView = findViewById(R.id.tv_last_order_time);

        // 地区信息
        mLocationAreaView = findViewById(R.id.tv_location_area);
        mDetailedAddressView = findViewById(R.id.tv_detailed_address);
        mLatLngView = findViewById(R.id.tv_lat_lng);
    }

    @Override
    protected void initData() {
        // 获取Bundle参数
        Bundle args = getArguments();
        if (args != null) {
            mHasLicense = args.getBoolean(ARG_HAS_LICENSE, false);
        }
        
        // 更新布局显示
        updateLicenseLayout();
        
        // TODO: 加载详情数据
    }

    /**
     * 更新营业执照布局显示
     */
    private void updateLicenseLayout() {
        if (mHasLicense) {
            // 有营业执照：显示营业执照图、注册名称、营业执照号
            mLayoutHasLicenseContent.setVisibility(View.VISIBLE);
            mLayoutNoLicenseContent.setVisibility(View.GONE);
        } else {
            // 无营业执照：显示上传图片、备注
            mLayoutHasLicenseContent.setVisibility(View.GONE);
            mLayoutNoLicenseContent.setVisibility(View.VISIBLE);
        }
    }
}

